﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.Channel.Api.Entity;
using Pub.Class;

namespace GTS.Channel.Api.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-04-01 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class IVRDealDataHelper {
        /// <summary>
        /// 缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 存在时修改记录，否则添加记录
        /// </summary>
        /// <param name="iVRDealData">实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改或添加是否成功</returns>
        public static bool UpdateAndInsert(IVRDealData iVRDealData, string dbkey = "", string where = "", string[] delCache = null) {
            if (iVRDealData.SPCode.IsNullEmpty()  && iVRDealData.LinkID.IsNullEmpty()) return  false ;
            string insertSql = new SQL().Insert(IVRDealData._)
                .Value(IVRDealData._SPCode, iVRDealData.SPCode)
                .Value(IVRDealData._LinkID, iVRDealData.LinkID)
                .Value(IVRDealData._SMSKey, iVRDealData.SMSKey)
                .Value(IVRDealData._LongPhone, iVRDealData.LongPhone)
                .Value(IVRDealData._Mobile, iVRDealData.Mobile)
                .Value(IVRDealData._Price, iVRDealData.Price)
                .Value(IVRDealData._FeeTime, iVRDealData.FeeTime)
                .Value(IVRDealData._RequestDate, iVRDealData.RequestDate)
                .Value(IVRDealData._ResponseStatus, iVRDealData.ResponseStatus)
                .Value(IVRDealData._BeginTime, iVRDealData.BeginTime)
                .Value(IVRDealData._EndTime, iVRDealData.EndTime)
                .Value(IVRDealData._MountCode, iVRDealData.MountCode)
                .Value(IVRDealData._DeviceCode, iVRDealData.DeviceCode)
                .Value(IVRDealData._IMSI, iVRDealData.IMSI)
                .Value(IVRDealData._CityCode, iVRDealData.CityCode)
                .Value(IVRDealData._ChannelID, iVRDealData.ChannelID)
                .Value(IVRDealData._Box, iVRDealData.Box)
                .ToString();
            string updateSql = new SQL().Update(IVRDealData._)
                .Set(IVRDealData._SMSKey, iVRDealData.SMSKey)
                .Set(IVRDealData._LongPhone, iVRDealData.LongPhone)
                .Set(IVRDealData._Mobile, iVRDealData.Mobile)
                .Set(IVRDealData._Price, iVRDealData.Price)
                .Set(IVRDealData._FeeTime, iVRDealData.FeeTime)
                .Set(IVRDealData._RequestDate, iVRDealData.RequestDate)
                .Set(IVRDealData._ResponseStatus, iVRDealData.ResponseStatus)
                .Set(IVRDealData._BeginTime, iVRDealData.BeginTime)
                .Set(IVRDealData._EndTime, iVRDealData.EndTime)
                .Set(IVRDealData._MountCode, iVRDealData.MountCode)
                .Set(IVRDealData._DeviceCode, iVRDealData.DeviceCode)
                .Set(IVRDealData._IMSI, iVRDealData.IMSI)
                .Set(IVRDealData._CityCode, iVRDealData.CityCode)
                .Set(IVRDealData._ChannelID, iVRDealData.ChannelID)
                .Set(IVRDealData._Box, iVRDealData.Box)
                .Where("1=1").Where(new Where()
                    .And(IVRDealData._SPCode, iVRDealData.SPCode, Operator.Equal, true)
                    .And(IVRDealData._LinkID, iVRDealData.LinkID, Operator.Equal, true)
                ).ToString();
            string strSql = new SQL().From(IVRDealData._)
                .Select("1")
                .Where("1=1").Where(
                    new Where()
                    .And(IVRDealData._SPCode, iVRDealData.SPCode, Operator.Equal, true)
                    .And(IVRDealData._LinkID, iVRDealData.LinkID, Operator.Equal, true)
                    .ToString()
                ).ToString();
            if (!where.IsNullEmpty()) { updateSql += where; strSql += where; }
            strSql = "if exists(" + strSql + ") " + updateSql + " else " + insertSql;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "IVRDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(IVRDealDataCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 存在时修改记录，否则添加记录
        /// </summary>
        /// <param name="iVRDealData">实体类</param>
        /// <returns>修改或添加是否成功</returns>
        public static bool UpdateAndInsert(IVRDealData iVRDealData, string dbkey = "") {
            return UpdateAndInsert(iVRDealData, dbkey, "", null);
        }
    }
}

